package controller

import (
	"github.com/gofiber/fiber"
	jsoniter "github.com/json-iterator/go"
	"gowork/1+NService/model"
	"strconv"
)

type NServiceController struct {
	NService *model.NService
}

func (nsc *NServiceController)ImportNServiceInfo(c *fiber.Ctx){

	nsc.NService = new(model.NService)

	staffIdStr := c.FormValue("staffId")
	staffId, err := strconv.ParseInt(staffIdStr,10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	var waiterInfoList []string
	waiterInfoListStr := c.FormValue("waiterInfoList")
	dErr := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(waiterInfoListStr), &waiterInfoList)
	if dErr != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": dErr.Error(),
		})
		return
	}

	res , err := nsc.NService.ImportNServiceInfo(staffId, waiterInfoList)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (nsc *NServiceController)Distribution(c *fiber.Ctx){

	nsc.NService = new(model.NService)

	nServiceIdStr := c.FormValue("nServiceId")
	nServiceId, err := strconv.ParseInt(nServiceIdStr,10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	staffIdStr := c.FormValue("staffId")
	staffId, err := strconv.ParseInt(staffIdStr,10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	staffName := c.FormValue("staffName")

	loginStaffIdStr := c.FormValue("loginStaffId")
	loginStaffId, err := strconv.ParseInt(loginStaffIdStr,10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := nsc.NService.Distribution(nServiceId, staffId, staffName, loginStaffId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (nsc *NServiceController)QueryNServiceInfo(c *fiber.Ctx){

	nsc.NService = new(model.NService)

	staffIdStr := c.FormValue("staffId")
	staffId, err := strconv.ParseInt(staffIdStr,10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := nsc.NService.QueryNServiceInfo(staffId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": "200",
		"data": res,
		"message": "成功",
	})
	return
}
